#########################################################################
#				xenstoreutils.py : XenStore Utilities					#
#																		#
#########################################################################

#!/usr/bin/env python

# Imports
from xen.xend.xenstore.xsutil import *
from xen.xend.xenstore.xswatch import *

# Function to read a XenStore value
def ReadVal(myPath):
	
	# Get a XenStore handle
	xs = xshandle()
	
	# Read in the path
	xsTrans = xs.transaction_start()
	val = xs.read(xsTrans, myPath)
	xs.transaction_end(xsTrans)		
	
	# Return
	return val

# Function to list a XenStore directory
def ListDir(myPath):
	
	# Get a XenStore handle
	xs = xshandle()
	
	# Read in the path
	xsTrans = xs.transaction_start()
	itemList = xs.ls(xsTrans, myPath)
	xs.transaction_end(xsTrans)		
	
	# Return
	return itemList

# Function to enumerate a XenStore directory and return a dictionary of items
def EnumerateDir(myPath):
	
	# Create a dictionary to return
	myDict = {}
	
	# Get a list of the items in the path
	itemList = ListDir(myPath)
	
	# Iterate over all the items and read their values
	for thisItem in itemList:
	
		# Get this item's value
		thisValue = ReadVal(myPath + '/' + thisItem)
		
		# Save if it's not blank
		if thisValue != '':
			myDict[thisItem] = thisValue
	
	# Return
	return myDict

